package com.hzj.auto;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * @author：hzj
 * @create: 2021-03-05 16:47
 * 使用Mybatis-Plus自动生成代码 启动类
 */
public class AutoGeneratorTest {

    public static void main(String[] args) {
//数据库连接信息
        String Url = "jdbc:mysql://localhost:3306/hzj_live?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8";
        String DriverName = "com.mysql.cj.jdbc.Driver";
        String Username = "root";
        String Password = "123";
//作者名称
        String Author = "hzj";
//生成的父包
        String Parent = "com.hzj";
//实体类包
        String Entity = "pojo";
//mapper接口包
        String Mapper = "mapper";
//controller包
        String Controller = "controller";
//service包
        String Service = "service";
//service实现类包
        String ServiceImpl = "service.impl";
//表名称,生成多个表可以使用字符串数组：new String[]{"表1","表2"}
        String[] Include = new String[]{"live_users","mate_selection_criteria"};
//创建Generator对象
        AutoGenerator autoGenerator = new AutoGenerator();
//数据源信息
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl(Url);
        dataSourceConfig.setDriverName(DriverName);
        dataSourceConfig.setUsername(Username);
        dataSourceConfig.setPassword(Password);
        autoGenerator.setDataSource(dataSourceConfig);
//全局配置
        GlobalConfig globalConfig = new GlobalConfig();
//生成路径
        globalConfig.setOutputDir(System.getProperty("user.dir") + "/data");
        globalConfig.setOpen(false);
        globalConfig.setAuthor(Author);
        globalConfig.setServiceName("%sService");
        autoGenerator.setGlobalConfig(globalConfig);
//包信息
        PackageConfig packageConfig = new PackageConfig();
//生成的父包
        packageConfig.setParent(Parent);
//实体类包
        packageConfig.setEntity(Entity);
//mapper接口包
        packageConfig.setMapper(Mapper);
//controller包
        packageConfig.setController(Controller);
//service包
        packageConfig.setService(Service);
//service实现类包
        packageConfig.setServiceImpl(ServiceImpl);
        autoGenerator.setPackageInfo(packageConfig);
//配置策略
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
//映射生成的表名，不写默认就是数据库的所有表
        strategyConfig.setInclude(Include);
        strategyConfig.setEntityLombokModel(true);
        autoGenerator.setStrategy(strategyConfig);

        autoGenerator.execute();
    }
}